<?php
/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/10/11
 * Time: 17:05
 */

namespace app\api\controller;


use app\api\BaseController;
use app\common\model\Area;

/**
 * @title 收货地址
 */
class Address extends BaseController
{

    /**
     * @title 地区三级联动
     * @description
     * @author fuzz
     * @url /Address/areaInfo
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     * @param string none none
     * @return {"code":200,"msg":"succeess"}
     */
    public function areaInfo(){
        $area_mod=new Area();
        $region_list = $area_mod->getAreaArrayForJson();
        return json_success_return($region_list);
    }

    /**
     * @title 地址新增
     * @description
     * @author fuzz
     * @url /Address/add
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     * @param string true_name 收貨人
     * @param string city_id 市
     * @param string area_id 区
     * @param string address 地址
     * @param string longitude 经度
     * @param string latitude 纬度
     * @param string tel_phone 手机号
     * @param string mob_phone 电话号
     * @param string is_default 是否默认1是0否 0
     * @param string area_info 省名称
     * @return {"code":200,"msg":"succeess"}
     */
    public function add(){
        $data = array(
            'member_id' => $this->user_id,
            'address_realname' => input('post.true_name'),
            'area_id' => input('post.area_id'),
            'city_id' => input('post.city_id'),
            'address_detail' => input('post.address'),
            'address_longitude' => input('post.longitude'),
            'address_latitude' => input('post.latitude'),
            'address_tel_phone' => input('post.tel_phone'),
            'address_mob_phone' => input('post.mob_phone'),
            'address_is_default' => input('post.is_default') == 1 ? 1 : 0,
            'area_info' => input('post.area_info'),
        );
        $memberaddress_validate = validate('memberaddress');
        if (!$memberaddress_validate->scene('add')->check($data)) {
            return json_fail_return($memberaddress_validate->getError());
        }

        $address_model=new \app\common\model\Address();
        $result = $address_model->addAddress($data);
        if ($result) {
            return json_success_return($result,'新增成功');
        } else {
            return json_fail_return(lang('ds_common_save_fail'));
        }
    }

    /**
     * @title 地址列表
     * @description
     * @author fuzz
     * @url /Address/add
     * @method GET
     *
     * @code 200 成功
     * @code 201 失败
     *
     * @param string none none
     * @return {"code":200,"msg":"succeess"}
     */
    public function select(){
        $address_model=new \app\common\model\Address();
        $default = input('post.is_default');
        $address_list = $address_model->getAddressList(array('member_id'=>$this->user_id),$default);
        return json_success_return($address_list);
    }

    /**
     * @title 地址编辑
     * @description
     * @author fuzz
     * @url /Address/edit
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     * @param string address_id 地址id
     * @param string true_name 收貨人
     * @param string city_id 市
     * @param string area_id 区
     * @param string address 地址
     * @param string longitude 经度
     * @param string latitude 纬度
     * @param string tel_phone 手机号
     * @param string mob_phone 电话号
     * @param string is_default 是否默认1是0否 0
     * @param string area_info 省名称
     * @return {"code":200,"msg":"succeess"}
     */
    public function edit(){
        $address_id = intval(input('param.address_id'));
        if (0 >= $address_id) {
            return ds_json_encode(10001,lang('param_error'));
        }
        $address_model=new \app\common\model\Address();
        $address = $address_model->getAddressInfo(array('member_id' => $this->user_id, 'address_id' => $address_id));
        if (empty($address)) {
            return json_fail_return(lang('address_does_not_exist'));
        }
        $data = array(
            'address_realname' => input('post.true_name'),
            'area_id' => input('post.area_id'),
            'city_id' => input('post.city_id'),
            'address_detail' => input('post.address'),
            'address_longitude' => input('post.longitude'),
            'address_latitude' => input('post.latitude'),
            'address_tel_phone' => input('post.tel_phone'),
            'address_mob_phone' => input('post.mob_phone'),
            'address_is_default' => input('post.is_default') == 1 ? 1 : 0,
            'area_info' => input('post.area_info'),
        );
        $memberaddress_validate = validate('memberaddress');
        if (!$memberaddress_validate->scene('edit')->check($data)) {
            return json_fail_return($memberaddress_validate->getError());
        }

        $result = $address_model->editAddress($data,array('member_id' => $this->user_id, 'address_id' => $address_id));
        if ($result) {
            return json_success_return([], '修改成功');
        } else {
            return json_fail_return(lang('ds_common_save_fail'));
        }
    }

    /**
     * @title 地址删除
     * @description
     * @author fuzz
     * @url /Address/del
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     * @param string address_id 地址id
     * @return {"code":200,"msg":"succeess"}
     */
    public function del() {
        $address_id = intval(input('param.address_id'));
        if (0 >= $address_id) {
            return json_fail_return(lang('empty_error'));
        }
        $address_model=new \app\common\model\Address();
        $condition = array();
        $condition['address_id'] = $address_id;
        $condition['member_id'] = $this->user_id;
        $result = $address_model->delAddress($condition);
        if ($result) {
            return json_success_return([], '删除成功');
        } else {
            return json_fail_return('删除失败');
        }
    }

    /**
     * @title 设置为默认地址
     * @description
     * @author fuzz
     * @url /Address/default_address
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     * @param string address_id 地址id
     * @return {"code":200,"msg":"succeess"}
     */
    public function default_address() {
        $address_id = intval(input('param.address_id'));
        if (0 >= $address_id) {
            return json_fail_return(lang('empty_error'));
        }
        $address_model=new \app\common\model\Address();
        $user_id = $this->user_id;
        $result = $address_model->defaultAddress($user_id,$address_id);
        if ($result) {
            return json_success_return([], '修改成功');
        } else {
            return json_fail_return('修改失败');
        }
    }

    /**
     * @title 获取默认地址
     * @description
     * @author fuzz
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function servicePrepare()
    {
        if (empty(input('post.'))) {
            return json_fail_return(lang('wrong_argument'));
        }

        $result['address_info'] = model('address')->getDefaultAddressInfo(array('member_id' => $this->user_id));

        json_success_return($result);
        
    }

    /**
     * 获取省市区
     */
    public function get_area_list()
    {
        $areaid = input('param.areaid');
        
        $area_model = model('area');
         #获取手机顶级分类
        $where = array();
        $where['area_parent_id'] = $areaid;
        $field = 'area_id,area_name';
        $area_class = $area_model->getarea_list($where, $field);
        return json_success_return($area_class);
    }


}
